// 引入mysql的配置文件
const db = require('../config/db');
const {
    DataTypes
} = require("sequelize");
const uuid = require('node-uuid');
// 引入sequelize对象
const Sequelize = db.sequelize;
// 引入数据表模型
const category = require('../schema/category.js')(Sequelize, DataTypes);
const article = require('../schema/article.js')(Sequelize, DataTypes);
category.hasMany(article, {
    foreignKey: 'category_id'
});
category.sync({
    force: false
}); //自动创建表

class categoryModel {
    /**
     * 创建学生表模型
     * @param data
     * @returns {Promise<*>}
     */
    static async createCategory(data) {
        return await category.create({
            name: data.name, //类别名称
            id: uuid.v1()
        });
    }

    /**
     * 查询全部菜单
     * @param  {}
     * @returns {Promise<Model>}
     */
    static async getCategory() {
        return await category.findAll()
    }

    /**
     * 删除菜单
     * @param {*} id 
     * @returns 
     */
    static async deleteCategory(id) {
        return await category.destroy({
            where: {
                id
            }
        })
    }
    /**
     * 查询学生信息的详情
     * @param id 学生信息ID
     * @returns {Promise<Model>}
     */
    static async getCategoryDetail(query) {
        return await category.findOne({
            where: {
                ...query
            }
        });
    }
}

module.exports = categoryModel;